#!/usr/bin/python
# -*- coding: UTF-8 -*
"""
@author：li-boss
@file_name: in_service.py
@create date: 2019/11/23 10:43 
@blog https://leezhonglin.github.io
@csdn https://blog.csdn.net/qq_33196814
@file_description：修改服务配置文件
"""

import os


def create_new_file(file_name, new_file_name, old_str, new_str):
    """
    修改修改python配置文件
    :param file_name: 被打开的文件的路径
    :param new_file_name: 修改后生成的新文件的路径
    :param old_str: 旧的字符串
    :param new_str: 新字符串
    :return:
    """
    # encoding根据具体的系统来确定 如果是windows创建的记事本请用gbk
    fo = open(file_name, 'r+')
    results = []
    link = fo.readlines()
    for fileLine in link:
        str = fileLine.replace(old_str, new_str)
        results.append(str)
        # print('>>', fileLine.strip())
    fo.close()

    # 获取的新数据写入新文件中
    # encoding根据具体的系统来确定 如果是windows创建的记事本请用gbk
    new_fo = open(new_file_name, 'w+')
    for str in results:
        new_fo.write(str)
    new_fo.close()
    # print('修改完成')


if __name__ == "__main__":
    # 当前的工作目录
    # path = os.getcwd()
    # 打包用的路径修改路径
    path = os.path.abspath('..') + r"\dist"

    # VHost_CONF配置路径
    csips_server_conf = path + r"\li-boss-service-def.xml"
    # 定义旧的字符串
    old_str = "%BASE_PATH%"

    new_server_file = path + "\li-boss-service.xml"

    # 生成vhost配置文件
    create_new_file(csips_server_conf, new_server_file, old_str, path)
